package de.szut.scene.helper;

import de.szut.main.ResourcesManager;
import de.szut.scene.scenes.GameScene;
import de.szut.scene.scenes.LoadingScene;
import de.szut.scene.scenes.MenuScene;
import de.szut.scene.scenes.SplashScene;
import org.andengine.engine.Engine;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.ui.IGameInterface;

/**
 * Created by dzebcuks on 22.08.13.
 */
public class SceneManager {
    //---------------------------------------------
    // SCENES
    //---------------------------------------------

    private BaseScene splashScene;
    private BaseScene menuScene;
    private BaseScene gameScene;
    private BaseScene loadingScene;

    //---------------------------------------------
    // VARIABLES
    //---------------------------------------------

    private static final SceneManager INSTANCE = new SceneManager();

    private SceneType currentSceneType = SceneType.SCENE_SPLASH;

    private BaseScene currentScene;

    private Engine engine = ResourcesManager.getInstance().getEngine();

    //---------------------------------------------
    // CLASS LOGIC
    //---------------------------------------------

    public void setScene(BaseScene scene) {
        engine.setScene(scene);
        currentScene = scene;
        currentSceneType = scene.getSceneType();
    }

    public void setScene(SceneType sceneType) {
        switch (sceneType) {
            case SCENE_MENU:
                setScene(menuScene);
                break;
            case SCENE_GAME:
                setScene(gameScene);
                break;
            case SCENE_SPLASH:
                setScene(splashScene);
                break;
            case SCENE_LOADING:
                setScene(loadingScene);
                break;
            default:
                break;
        }
    }


    public void createGameScene() {
        setScene(loadingScene);
        this.disposeMenuScene();
        engine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() {
            @Override
            public void onTimePassed(TimerHandler pTimerHandler) {
                engine.unregisterUpdateHandler(pTimerHandler);
                disposeLoadingSecene();
                ResourcesManager.getInstance().loadGameResources();
                gameScene = new GameScene();
                setScene(gameScene);
            }
        }));
    }

    public void createMenuScene() {
        this.disposeSplashScene();
        ResourcesManager.getInstance().loadMenuResources();
        this.menuScene = new MenuScene();
        this.loadingScene = new LoadingScene();
        this.setScene(menuScene);
    }

    public void backToMenuScene(){
        ResourcesManager.getInstance().loadMenuResources();
        this.menuScene = new MenuScene();
        this.loadingScene = new LoadingScene();
        this.setScene(menuScene);
    }

    public void createSplashScene(IGameInterface.OnCreateSceneCallback pOnCreateSceneCallback) {
        ResourcesManager.getInstance().loadSplashScreen();
        splashScene = new SplashScene();
        this.setScene(splashScene);
        pOnCreateSceneCallback.onCreateSceneFinished(splashScene);
    }

    private void disposeSplashScene() {
        ResourcesManager.getInstance().unloadSplashScreen();
        splashScene.disposeScene();
        splashScene = null;
    }

    private void disposeMenuScene() {
        ResourcesManager.getInstance().unloadMenuScreen();
        menuScene.disposeScene();
        menuScene = null;
    }

    public void disposeGameSecne() {
        ResourcesManager.getInstance().unloadGameScreen();
        gameScene.disposeScene();
        gameScene = null;
        backToMenuScene();
    }

    private void disposeLoadingSecene() {
        loadingScene.disposeScene();
        loadingScene = null;
    }

    //---------------------------------------------
    // GETTERS AND SETTERS
    //---------------------------------------------

    public static SceneManager getInstance() {
        return INSTANCE;
    }

    public SceneType getCurrentSceneType() {
        return currentSceneType;
    }

    public BaseScene getCurrentScene() {
        return currentScene;
    }

    public BaseScene getGameScene() {
        return gameScene;
    }
}
